﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ProjectEulerSolutions
{
    /*
     * Each of the six faces on a cube has a different digit (0 to 9) written on it; the same is done to a second cube. By placing the two cubes side-by-side in different positions we can form a variety of 2-digit numbers.

For example, the square number 64 could be formed:

In fact, by carefully choosing the digits on both cubes it is possible to display all of the square numbers below one-hundred: 01, 04, 09, 16, 25, 36, 49, 64, and 81.

For example, one way this can be achieved is by placing {0, 5, 6, 7, 8, 9} on one cube and {1, 2, 3, 4, 8, 9} on the other cube.

However, for this problem we shall allow the 6 or 9 to be turned upside-down so that an arrangement like {0, 5, 6, 7, 8, 9} and {1, 2, 3, 4, 6, 7} allows for all nine square numbers to be displayed; otherwise it would be impossible to obtain 09.

In determining a distinct arrangement we are interested in the digits on each cube, not the order.

{1, 2, 3, 4, 5, 6} is equivalent to {3, 6, 4, 1, 2, 5}
{1, 2, 3, 4, 5, 6} is distinct from {1, 2, 3, 4, 5, 9}

But because we are allowing 6 and 9 to be reversed, the two distinct sets in the last example both represent the extended set {1, 2, 3, 4, 5, 6, 9} for the purpose of forming 2-digit numbers.

How many distinct arrangements of the two cubes allow for all of the square numbers to be displayed?

     * */
    class Problem90 : IProblem
    {
        public string Calculate()
        {
            List<char> numbers = new List<char>() { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };

            var q = CommonFunctions.GetCombination(numbers, 6).ToList();


            int count = 0;

            for (int i = 0; i < q.Count; i++)
            {
                for (int j = i + 1; j < q.Count; j++)
                {
                    if (HasSquares(q[i], q[j]))
                    {
                        //if ((q[i].Contains('9') && !q[i].Contains('6')) || (q[j].Contains('9') && !q[j].Contains('6')))
                        //{
                        //    continue;
                        //}
                        count++;
                        Console.WriteLine("{0} - {1}", q[i], q[j]);
                    }
                }
            }


            return count.ToString();
        }

        public bool HasSquares(string first, string second)
        {
            //01 04 09 16 25 36 49 64 81

            if (!((first.Contains('0') && second.Contains('1')) || (second.Contains('0') && first.Contains('1'))))
                return false;
            if (!((first.Contains('0') && second.Contains('4')) || (second.Contains('0') && first.Contains('4'))))
                return false;
            if (!((first.Contains('0') && (second.Contains('6') || second.Contains('9'))) || (second.Contains('0') && (first.Contains('6') || first.Contains('9')))))
                return false;
            if (!((first.Contains('1') && (second.Contains('6') || second.Contains('9'))) || (second.Contains('1') && (first.Contains('6') || first.Contains('9')))))
                return false;
            if (!((first.Contains('2') && second.Contains('5')) || (second.Contains('2') && first.Contains('5'))))
                return false;
            if (!((first.Contains('3') && (second.Contains('6') || second.Contains('9'))) || (second.Contains('3') && (first.Contains('6') || first.Contains('9')))))
                return false;
            if (!((first.Contains('4') && (second.Contains('6') || second.Contains('9'))) || (second.Contains('4') && (first.Contains('6') || first.Contains('9')))))
                return false;
            if (!(((first.Contains('6') || first.Contains('9')) && second.Contains('4')) || ((second.Contains('6') || second.Contains('9')) && first.Contains('4'))))
                return false;
            if (!((first.Contains('8') && second.Contains('1')) || (second.Contains('8') && first.Contains('1'))))
                return false;

            return true;
        }
    }
}
